package com.zhiche.lisa.tms.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.tms.dao.mapper.AbnormalPodReasonMapper;
import com.zhiche.lisa.tms.dao.mapper.PodReasonMapper;
import com.zhiche.lisa.tms.dao.model.AbnormalPodReason;
import com.zhiche.lisa.tms.dao.model.PodReason;
import com.zhiche.lisa.tms.dao.model.ShipOrderItem;
import com.zhiche.lisa.tms.dto.PodReasonDTO;
import com.zhiche.lisa.tms.dto.ReceiptDTO;
import com.zhiche.lisa.tms.service.AbnormalPodReasonService;
import com.zhiche.lisa.tms.service.IShipOrderItemService;
import com.zhiche.lisa.tms.service.PodReasonService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author : suohao
 * Date: 2019/7/29
 */
@Service
public class PodReasonServiceImpl extends ServiceImpl<PodReasonMapper, PodReason> implements PodReasonService {

    @Autowired
    IShipOrderItemService iShipOrderItemService;

    @Override
    public List<Map<String, Object>> getpodReasonList(Map<String,String> map) {
        EntityWrapper<PodReason> ew = new EntityWrapper<>();
       if (!StringUtils.isEmpty(map.get("orderNo"))) {
           ew.eq("orderNo", map.get("orderNo"));
       }
        List<Map<String, Object>> maps = new ArrayList<>();
        int count=baseMapper.selectReason(map.get("orderNo"));
        if (count==0){
            return maps;
        }
        return baseMapper.selectReasonListNoTenant(ew);
    }

    @Override
    public void synPodStatus(String param) {
        JSONArray jsa = JSONArray.parseArray(param);
        for (Object o : jsa) {
            JSONObject jsonObject = JSONObject.parseObject(o.toString());
            String orderGid = jsonObject.getString("ORDER_GID");
            String podStatus = jsonObject.getString("POD_STATUS");
            EntityWrapper<ShipOrderItem> shipOrderItemEntityWrapper=new EntityWrapper<>();
            shipOrderItemEntityWrapper.eq("order_no",orderGid.substring(7,orderGid.length())).eq("terminal","1").eq("is_delete",0);
            List<ShipOrderItem> shipOrderItems = iShipOrderItemService.selectListNotenant(shipOrderItemEntityWrapper);
            for (ShipOrderItem shipOrderItem : shipOrderItems) {
                if (!podStatus.equals(shipOrderItem.getReceiptStatus())){
                    shipOrderItem.setReceiptStatus(podStatus);
                    iShipOrderItemService.updateById(shipOrderItem);
                }
            }
        }
    }
}
